-- Author  : Kurapica
-- Create Date : 2010/07/19

do
	IGAS:NewAddon("IGAS_Studio")

	local form = CreateFrame(Widget.AdvanceForm, "IGAS_Studio_TablePropertySet", coreF)
	form.Caption = L["Complex property settings"]
	form.btnUp.Visible = true
	form.btnUp.Enabled = true
	form.btnUp.Text = L["Max"]
	form.btnDown.Visible = false

	local lst = form.List
	local dtg = form.DataGrid

	local _Frame = nil
	local _Prop = nil
	local _Value = nil

	form:RegisterEvent("IGAS_STUDIO_PROPERTY_ADVANCE")

	dtg.RowCount = 1
	dtg.Cells(1, 1).Text = L["Value"]

	-- Special PropertyType
	local _ColorInfo = nil
	local function _Cb_Color(red, green, blue, alpha)
		local color = {
			red = red,
			green = green,
			blue = blue,
			alpha = alpha,
		}
		_ColorInfo.Store[_ColorInfo.Name] = color
	end
	
	local _SpecialType = {
		-- Color
		[IGAS:GetPropertyType("Color")] = function(info)
			local color = info.Store[info.Name] or {}
			_ColorInfo = info
			
			return IGAS:ColorPick(_Cb_Color, color.red, color.green, color.blue, color.alpha)
		end,
		
		--[[TexturePath
		[IGAS:GetPropertyType("TexturePath")] = {
		}--]]
	}
	
	-- Scripts
	local function AddItem(name)
		if name == nil then
			return
		end
		
		name = tonumber(name) or name
		
		for _, info in pairs(lst.Keys) do
			if info.Name == name then				
				return
			end
		end
		
		local info = {
			Store = _Value,
			Name = name,
		}

		lst:AddItem(info, tostring(name))
	end
	
	local function RemoveItem(info)
		info.Store[info.Name] = nil
		
		lst:RemoveItem(info)
	end
	
	local function SetMax(count)
		count = tonumber(count)
		if count and count >= 0 then
			for i =1, count do
				AddItem(i)
			end

			for _, info in pairs(lst.Keys) do
				if type(info.Name) == "number" and info.Name > count then
					RemoveItem(info)
				end
			end
		end
	end

	local function LoadList()
		for i, v in pairs(_Value) do
			if (type(i) == "string" or type(i) == "number") and (type(v) == "string") then
				local info = {
					Store = _Value,
					Name = i,
				}
				lst:AddItem(info, tostring(i))
			end
		end
	end

	local function LoadMembers(member, store, pre)
		pre = pre or ""
		store = store or _Value

		for name, subType in pairs(member) do
			subType = IGAS:GetPropertyType(subType)

			if subType then
				-- Special Checking
				if _SpecialType[subType] then
					local info = {
						Store = store,
						Name = name,
						Type = subType,
					}
				elseif subType.Type == "number" or subType.Type == "string" or subType.Type == "boolean" or subType.Type == "enum" then
					local info = {
						Store = store,
						Name = name,
						Type = subType,
					}
					
					if pre ~= "" then
						name = pre.."."..name
					end
					
					lst:AddItem(info, name)
				elseif subType.Type == "table" then
					if subType.Members then
						local subStore = store[name] or {}
						
						if pre ~= "" then
							name = pre.."."..name
						end
						
						LoadMembers(subType.Members, subStore, name)
					end
				end
			end
		end
	end

	function form:IGAS_STUDIO_PROPERTY_ADVANCE(frame, propName)
		_Frame = frame
		_Prop = propName
		lst:Clear()
		dtg.Cells(1, 2).CellType = "Label"
		dtg.Cells(1, 2).Text = ""

		if frame and propName then
			_Value = GetTrueFrame(frame)[propName] or {}

			local property = GetTrueObject(frame).Property[propName]
			local propType = IGAS:GetPropertyType(property.Type)

			if _SpecialType[propType] and type(_SpecialType[propType]) == "function" then
				-- Special PropType, such like color, just show a color picker
				local info = {
					Store = GetTrueFrame(frame),
					Name = propName,
					Type = propType,
				}
				
				return _SpecialType[propType](info)
			end
			
			if propType and propType.Type == "table" then
				if propType.Members then
					form.btnAdd.Enabled = false
					form.btnRemove.Enabled = false
					form.btnUp.Visible = false

					LoadMembers(propType.Members)
				else
					form.btnAdd.Enabled = true
					form.btnRemove.Enabled = true
					form.btnUp.Visible = true

					LoadList()
				end
				self.Visible = true
			end
		end
	end

	function lst:OnItemChoosed(info, name)
		dtg.Info = nil

		if not info then
			return
		end
		
		if info.Type then
			if _SpecialType[info.Type] then
				if type(_SpecialType[info.Type]) == "function" then
					dtg.Cells(1, 2).CellType = "Advance"
				elseif type(_SpecialType[info.Type]) == "table" then
					-- Mean list
					dtg:SetCellType(1, 2, "ComboBox", _SpecialType[info.Type])
				end
			elseif info.Type.Type == "number" then
				dtg.Cells(1, 2).CellType = "Number"
			elseif info.Type.Type == "string" then
				dtg.Cells(1, 2).CellType = "String"
			elseif info.Type.Type == "boolean" then
				dtg.Cells(1, 2).CellType = "Boolean"
			elseif info.Type.Type == "enum" then
				dtg:SetCellType(1, 2, "ComboBox", info.Type.EnumType)
			end
		else
			dtg.Cells(1, 2).CellType = "String"
		end
		
		dtg.Cells(1, 2).Value = info.Store[info.Name]

		dtg.Info = info
	end

	function dtg:OnCellValueChanged(row, column, value)
		if row == 1 and column == 2 and self.Info then
			self.Info.Store[self.Info.Name] = value
		end
	end
	
	function dtg:OnAdvance(row, column)
		if row == 1 and column == 2 and self.Info and _SpecialType[self.Info.Type] and type(_SpecialType[self.Info.Type]) == "function" then
			return _SpecialType[self.Info.Type](self.Info)
		end
	end

	function form:OnOkayClick()
		GetTrueFrame(_Frame)[_Prop] = _Value		
		
		self.Visible = false
	end

	function form:OnUpClick()
		return IGAS:MsgBox(L["Set the max count of the list"], "ic", SetMax)
	end

	function form:OnAddClick()
		return IGAS:MsgBox(L["Add a key for an item(string or number)"], "ic", AddItem)
	end

	function form:OnRemoveClick()
		if lst:GetSelectedItemIndex() > 0 then
			dtg.Info = nil

			-- do clear
			dtg.Cells(1, 2).CellType = "Label"
			dtg.Cells(1, 2).Text = ""

			RemoveItem(lst:GetSelectedItemValue())
		end
	end

	function form:OnHide()
		_Frame = nil
		_Prop = nil
		_Value = nil
		dtg.Info = nil

		-- do clear
		dtg.Cells(1, 2).CellType = "Label"
		dtg.Cells(1, 2).Text = ""

		lst:Clear()
	end
end